﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace _6_03_ADO非查询操作
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //根据DataAdapter的Command来添加数据
        private void button1_Click(object sender, EventArgs e)
        {
            string connStr = "server=.;database=TestDb;uid=sa;pwd=200234.A";
            SqlConnection sqlconn = new SqlConnection(connStr);//创建连接对象
            sqlconn.Open();//连接数据库
            string cmd = $"insert into Member(MemberAccount,MemberPwd,MemberName,MemberPhone) values({this.textBox1.Text},{this.textBox2.Text},{this.textBox3.Text},{this.textBox4.Text})";

            SqlDataAdapter adp = new SqlDataAdapter(cmd, sqlconn);
            int count = adp.SelectCommand.ExecuteNonQuery();//执行非查询操作
            if (count > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }

            sqlconn.Close();//关闭连接
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string connStr = "server=.;database=TestDb;uid=sa;pwd=200234.A";
            SqlConnection sqlconn = new SqlConnection(connStr);//创建连接对象
            sqlconn.Open();//连接数据库
            string cmd = $"insert into Member(MemberAccount,MemberPwd,MemberName,MemberPhone) values({this.textBox1.Text},{this.textBox2.Text},{this.textBox3.Text},{this.textBox4.Text})";

            //SqlDataAdapter adp = new SqlDataAdapter(cmd, sqlconn);
            //int count = adp.SelectCommand.ExecuteNonQuery();//执行非查询操作

            SqlCommand sqlc = new SqlCommand(cmd, sqlconn);
            int count = sqlc.ExecuteNonQuery();//执行非查询操作
            if (count > 0)
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("添加失败");
            }

            sqlconn.Close();//关闭连接

        }
    }
}
